package cn.pet.service.manage.controller;


import cn.pet.service.manage.common.Response;
import cn.pet.service.manage.entity.PetComment;
import cn.pet.service.manage.entity.api.Result;
import cn.pet.service.manage.entity.dto.CommentDTO;
import cn.pet.service.manage.entity.dto.CommentSearch;
import cn.pet.service.manage.entity.vo.CommentVO;
import cn.pet.service.manage.mapstruct.CommentMapstruct;
import cn.pet.service.manage.service.PetCommentFileService;
import cn.pet.service.manage.service.PetCommentService;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 评论表 前端控制器
 * </p>
 *
 * @author 
 * @since 2023-02-11
 */
@RestController
@RequestMapping("/manage/petComment")
public class PetCommentController {

    @Resource
    private PetCommentService petCommentService;

    @Resource
    private PetCommentFileService petCommentFileService;


    @GetMapping("list")
    public Object list(CommentSearch search){
        return Result.ok("success",new PageInfo<>(petCommentService.listByPage(search)));
    }

    /**
     * 根据商品ID查询评论列表
     * @param goodsId
     * @return
     */
    @GetMapping("listByGoodsId/{goodsId}")
    public Object listByGoodsId(@PathVariable("goodsId") Integer goodsId){
        return Result.ok("success",petCommentService.listByGoodsId(goodsId));
    }

    /**
     * 添加评论
     * @param commentDTO
     * @return
     */
    @PostMapping("add")
    @Transactional
    public Object add(@RequestBody CommentDTO commentDTO){
        PetComment petComment = new PetComment();
        petComment = CommentMapstruct.INSTANCE.commonDtoToPetComment(commentDTO,petComment);
        if (!petCommentService.save(petComment)){
            return Result.error("添加失败");
        }
        //todo:上传图片
        if (!petCommentFileService.saveBatch(commentDTO.getPetCommentFiles())){
            return Result.error("添加失败");
        }
        return Result.ok("添加成功");
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public Object delete(@PathVariable("id") Integer id){
        if (!petCommentService.removeById(id)){
            return Result.error("删除失败");
        }
        return Result.ok("删除成功");
    }



}
